package org.lwd.datalink.core.runtime.rest.resources;

import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.lwd.datalink.domain.enent.EsColumnSyncEvent;
import org.lwd.datalink.event.EventBusFactory;
import org.lwd.datalink.utils.FutureCallback;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author lwd
 */
@Slf4j
@Path("/es")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class EsOperatorResource {

    @POST
    @Path("/syncCloumn/{mediaSourceId}")
    public Object syncCloumns(@PathParam("mediaSourceId") Long mediaSourceId, final Map<String, Object> request){
        try {
            String sql = (String) request.get("sql");
            Long mappingId = Long.valueOf((Integer)request.get("mappingId"));
            EventBus eventBus = EventBusFactory.getEventBus();
            EsColumnSyncEvent event = new EsColumnSyncEvent(new FutureCallback(),mediaSourceId,mappingId,sql);
            eventBus.post(event);
            return  event.getCallback().get();
        } catch (InterruptedException e) {
            log.info("中断异常 {}",e);
        } catch (ExecutionException e) {
            log.info("执行异常 {}",e);
        }catch (Exception e){
            log.info("其他异常 {}",e);
        }
        return null;
    }



}
